package com.zp.business.impl.exam;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.exam.ExamItem;
import com.zp.business.entity.exam.bo.ExamItemBo;
import com.zp.business.entity.exam.vo.ExamItemVo;
import com.zp.business.mapper.exam.ExamItemMapper;
import com.zp.business.service.exam.IExamItemService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查申请子Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class ExamItemServiceImpl implements IExamItemService {

    private final ExamItemMapper baseMapper;

    /**
     * 查询检查申请子
     */
    @Override
    public ExamItemVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检查申请子列表(分页)
     */
    @Override
    public TableDataInfo<ExamItemVo> queryPageList (ExamItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamItem> lqw = buildQueryWrapper(bo);
        Page<ExamItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检查申请子列表(不分页)
     */
    @Override
    public List<ExamItemVo> queryList (ExamItemBo bo) {
        LambdaQueryWrapper<ExamItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamItem> buildQueryWrapper (ExamItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getApplyId()), ExamItem::getApplyId, bo.getApplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicItemId()), ExamItem::getClinicItemId, bo.getClinicItemId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ExamItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getExamPosition()), ExamItem::getExamPosition, bo.getExamPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getItemType()), ExamItem::getItemType, bo.getItemType());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), ExamItem::getItemCode, bo.getItemCode());
        lqw.eq(bo.getNum() != null, ExamItem::getNum, bo.getNum());
        lqw.eq(bo.getPrice() != null, ExamItem::getPrice, bo.getPrice());
        lqw.eq(bo.getCosts() != null, ExamItem::getCosts, bo.getCosts());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ExamItem::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ExamItem::getRemarks, bo.getRemarks());
        lqw.eq(bo.getRealDate() != null, ExamItem::getRealDate, bo.getRealDate());
        return lqw;
    }

    /**
     * 新增检查申请子
     */
    @Override
    public Boolean insertByBo (ExamItemBo bo) {
        ExamItem add = BeanUtil.toBean(bo, ExamItem.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改检查申请子
     */
    @Override
    public Boolean updateByBo (ExamItemBo bo) {
        ExamItem update = BeanUtil.toBean(bo, ExamItem.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (ExamItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检查申请子
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量保存检查明细
     *
     * @param examItemList
     * @return
     */
    @Override
    public List<ExamItem> batchExamItem (List<ExamItem> examItemList) {
        baseMapper.insertBatch(examItemList);
        return examItemList;
    }

    @Override
    public Boolean deleteByBo (String examApplyId) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("apply_id", examApplyId);
        return baseMapper.deleteByMap(columnMap) > 0;
    }


}
